package com.mg.admin.controller.admin;

import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.WxFansVO;
import com.mg.admin.bean.admin.WxMenuVO;
import com.mg.admin.controller.BaseController;
import com.mg.admin.page.PageBean;
import com.mg.admin.page.PageParam;
import com.mg.admin.service.admin.WxFansService;
import com.mg.admin.service.admin.WxMenuService;
import com.xiaoleilu.hutool.util.ImageUtil;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;

/**
 * 公众号粉丝管理
 * Created by jujunchen on 2017/9/24.
 */
@Controller
@RequestMapping("/admin/wx/fans/")
public class WxFansController extends BaseController {

    @Autowired
    private WxFansService wxFansService;

    @Autowired
    private WxMpService wxMpService;

    /**
     * 粉丝列表
     */
    @GetMapping(LIST)
    public void list(ModelMap modelMap){

    }

    @ResponseBody
    @PostMapping(PAGE)
    public Result<PageBean> page(WxFansVO wxFansVO){
        PageParam pageParam = new PageParam(wxFansVO.getStart(),wxFansVO.getLength());
        PageBean pageBean = wxFansService.queryForPage(wxFansVO,pageParam);
        return new Result<>(pageBean);
    }

    /**
     * 规范命名所有列表都要已list结尾
     * @param modelMap
     * @param lv
     * @param id
     */
    @GetMapping("distlist")
    public void distlist(ModelMap modelMap,int lv,Long id){
        modelMap.put("lv",lv);
        modelMap.put("id",id);
    }

    @ResponseBody
    @PostMapping("distpage")
    public Result<PageBean> distpage(WxFansVO wxFansVO){
        PageParam pageParam = new PageParam(wxFansVO.getStart(),wxFansVO.getLength());
        PageBean pageBean = wxFansService.distPageList(wxFansVO,pageParam);
        return new Result<>(pageBean);
    }

    public static void main(String[] args) {
        File srcImageFile = new File("E:\\target.jpg");
        File pressImgFile = new File("E:\\2.jpg");
        File newFile = new File("E:\\new.jpg");
        ImageUtil.pressImage(pressImgFile,srcImageFile,newFile,0,0,1.0f);
    }


   /* @GetMapping(ADD)
    public void add(ModelMap modelMap){
        List<DictVO> list = dictService.findByClassId(DICT_WX_MENU_ID);
        modelMap.put("ordersList",list);
    }

    @GetMapping(EDIT)
    public void edit(ModelMap modelMap,Long id){
        List<DictVO> list = dictService.findByClassId(DICT_WX_MENU_ID);
        modelMap.put("ordersList",list);
        WxMenuVO wxMenuVO = wxMenuService.findOneById(id);
        modelMap.put("bean",wxMenuVO);
    }

    @ResponseBody
    @PostMapping(SAVE)
    public Result save(WxMenuVO wxMenuVO){
        if (wxMenuVO.getId() ==null){
            wxMenuService.insertOne(wxMenuVO);
        }else {
            wxMenuService.updateOne(wxMenuVO);
        }
        return new Result();
    }

    @GetMapping(DELETE)
    @ResponseBody
    public void delete(Long id){
        wxMenuService.delete(id);
    }

    @ResponseBody
    @GetMapping("sync")
    public void sync(){
        List<WxMenuVO> list = wxMenuService.findAll(1);
        WxMenu menu = new WxMenu();
        List<WxMenuButton> topButtonList = new ArrayList<>();
        for (WxMenuVO vo : list){
            WxMenuButton button = new WxMenuButton();

            if (vo.getType().equals(WxConsts.BUTTON_CLICK)){
                button.setType(WxConsts.BUTTON_CLICK);
                button.setName(vo.getName());
                button.setKey(vo.getKeyCode());
            }else {
                button.setType(WxConsts.BUTTON_VIEW);
                button.setName(vo.getName());
                button.setUrl(vo.getUrl());
            }

            if (vo.getOrders().length() ==1){
                topButtonList.add(button);
                menu.getButtons().add(button);
            }else {
                int leftOrder = Integer.parseInt(vo.getOrders().substring(0,1));
                WxMenuButton topButton = topButtonList.get(leftOrder - 1);
                topButton.getSubButtons().add(button);
            }
        }
        try {
            wxMpService.getMenuService().menuCreate(menu);
        } catch (WxErrorException e) {
            logger.error(e.getMessage());
        }
    }*/



}
